package com.qf.business.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.goods.mapper.GoodsInfoMapper;
import com.qf.business.goods.service.*;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.goods.dto.GoodsInfoDto;
import com.qf.data.goods.dto.GoodsNormsDto;
import com.qf.data.goods.entity.GoodsImage;
import com.qf.data.goods.entity.GoodsInfo;
import com.qf.data.goods.entity.GoodsNormsMiddle;
import com.qf.data.goods.vo.input.GoodsInput;
import com.qf.data.goods.vo.output.GoodsFengMian;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (GoodsInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-07-12 15:44:54
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsImageService goodsImageService;

    @Autowired
    private GoodsNormsMiddleService goodsNormsMiddleService;

    @Autowired
    private GoodsNormsService goodsNormsService;

    @Autowired
    private GoodsNormsValService goodsNormsValService;


    @Override
    public List<GoodsFengMian> querySystemList() {
        return goodsInfoMapper.querySystemList();
    }

    @Override
    public int insert(GoodsInput input) {
        //保存商品
        GoodsInfo goodsInfo = QfBeanUtils.beanCopy(input,GoodsInfo.class);
        super.save(goodsInfo);

        //生成DTO对象
        GoodsInfoDto goodsInfoDto = QfBeanUtils.beanCopy(goodsInfo, GoodsInfoDto.class);

        //保存商品的图片信息
        String imagesJson = input.getImages();
        List<GoodsImage> images = JSON.parseArray(imagesJson,GoodsImage.class);

        //循环保存图片信息
        for (GoodsImage image : images) {
            image.setGid(goodsInfo.getId());
            //保存images对象
            goodsImageService.save(image);

            //将封面信息 设置到DTO对象中
            if (image.getType() == 1) {
                goodsInfoDto.setImage(image.getImage());
            }
        }


        //保存商品的规格信息
        String norms = input.getNorms();
        List<GoodsNormsMiddle> goodsNormsMiddles = JSON.parseArray(norms,GoodsNormsMiddle.class);

        //规格的DTO集合
        List<GoodsNormsDto> normsDtos = new ArrayList<>();
        goodsInfoDto.setNorms(normsDtos);

        //设置商品id
        for (GoodsNormsMiddle goodsNormsMiddle : goodsNormsMiddles) {
            goodsNormsMiddle.setGoodsId(goodsInfo.getId());
            goodsNormsMiddleService.save(goodsNormsMiddle);

            //创建规格的DTO
//            GoodsNormsDto goodsNormsDto = new GoodsNormsDto();
//            goodsNormsDto.setNormsName(goodsNormsService.getById(goodsNormsMiddle.getNormsId()).getNormsName());
//            goodsNormsDto.setNormsVal(goodsNormsValService.getById(goodsNormsMiddle.getValId()).getNormsVal());
//            normsDtos.add(goodsNormsDto);
        }

        //同步到ES

        return 1;
    }
}

